package com.android.plc.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.android.plc.R;
import com.android.plc.CONST.COMMAND;
import com.android.plc.classholder.BasicItem.Type;
import com.android.plc.classholder.ProfileDetail;
import com.android.plc.ui.OnWheelChangedListener;
import com.android.plc.ui.OnWheelScrollListener;
import com.android.plc.ui.adapter.ArrayWheelAdapter;
import com.android.plc.ui.view.WheelView;

public class ProfileDetailDialog extends AlertDialog implements android.view.View.OnClickListener
{
	private static final String TAG = "ProfileDetailDialog";
	OnWheelSelectListener mListener; // the callback
	private ProfileDetail detail = new ProfileDetail(Type.NORMAL_ITEM);
	// Time changed flag
	private boolean timeScrolled = false;
	private WheelView codeHome;
	private WheelView codeUnit;
	private WheelView codeCode;
	private final String[] mArrHomes;
	private final String[] mArrUnits;
	private final String[] mArrCommands;
	private final String[] mArrEmpty = { "", };
	private int mIndexHome = 0;
	private int mIndexUnit = 0;
	private int mIndexCode = 0;

	public ProfileDetailDialog(Context context)
	{
		super(context, R.style.PickDialogTheme);
		Resources resources = context.getResources();
		mArrHomes = resources.getStringArray(R.array.profile_room_codes);
		mArrUnits = resources.getStringArray(R.array.profile_devices_codes);
		mArrCommands = resources.getStringArray(R.array.profile_command);
	}

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_profiledetail);

		codeHome = (WheelView) findViewById(R.id.room_code);
		codeHome.setAdapter(new ArrayWheelAdapter<String>(mArrHomes));
		codeHome.setVisibleItems(5);

		codeUnit = (WheelView) findViewById(R.id.device_code);
		codeUnit.setAdapter(new ArrayWheelAdapter<String>(mArrUnits));
		codeUnit.setVisibleItems(5);

		codeCode = (WheelView) findViewById(R.id.command);
		codeCode.setAdapter(new ArrayWheelAdapter<String>(mArrCommands));
		codeCode.setVisibleItems(5);

		codeHome.addChangingListener(mHomeListener);
		codeUnit.addChangingListener(mUnitListener);
		codeCode.addChangingListener(mCodeListener);

		codeHome.addScrollingListener(mScrollListener);
		codeUnit.addScrollingListener(mScrollListener);
		codeCode.addScrollingListener(mScrollListener);

		final Button save = (Button) findViewById(R.id.edit_save);
		save.setOnClickListener(this);
		final Button cancel = (Button) findViewById(R.id.edit_cancel);
		cancel.setOnClickListener(this);
		setProfileDetail();
	}

	private void setProfileDetail()
	{
		if (mIndexHome == 16)
		{
			detail.home_unit = 0;
			detail.command = COMMAND.ALLUSERLTSON;
		} else if (mIndexHome >= 17)
		{
			detail.home_unit = 0;
			detail.command = COMMAND.ALLUSERLTSOFF;
		} else
		{
			detail.home_unit = mIndexHome << 4;
			if (mIndexUnit == 16)
			{
				detail.command = COMMAND.ALLHOMELTSON;
			} else if (mIndexUnit >= 17)
			{
				detail.command = COMMAND.ALLHOMEUNITOFF;
			} else
			{
				detail.home_unit += mIndexUnit;
				if (mIndexCode == 0)
				{
					detail.command = COMMAND.ON;
				} else
				{
					detail.command = COMMAND.OFF;
				}
			}
		}
		Log.d(TAG, " setProfileDetail " + detail);
	}

	public void onClick(View v)
	{
		if (v.getId() == R.id.edit_save && mListener != null)
		{
			Log.d(TAG, " select " + detail);
			mListener.onSelected(detail);
		}
		this.dismiss();
	}

	public void setSelectCodeListener(OnWheelSelectListener listener)
	{
		mListener = listener;
	}

	private OnWheelChangedListener mHomeListener = new OnWheelChangedListener()
	{
		public void onChanged(WheelView wheel, int oldValue, int newValue)
		{
			if (!timeScrolled)
			{
				mIndexHome = newValue;
				Log.d(TAG, " onChanged " + oldValue + " / " + newValue);
				if (oldValue < 16 && newValue >= 16)
				{
					mIndexUnit = 0;
					mIndexCode = 0;
					codeUnit.setAdapter(new ArrayWheelAdapter<String>(mArrEmpty));
					codeCode.setAdapter(new ArrayWheelAdapter<String>(mArrEmpty));
					codeUnit.setCurrentItem(0);
					codeCode.setCurrentItem(0);
				}

				if (oldValue >= 16 && newValue < 16)
				{
					mIndexUnit = 0;
					mIndexCode = 0;
					codeUnit.setAdapter(new ArrayWheelAdapter<String>(mArrHomes));
					codeCode.setAdapter(new ArrayWheelAdapter<String>(mArrCommands));
					codeUnit.setCurrentItem(0);
					codeCode.setCurrentItem(0);
				}
				setProfileDetail();
			}
		}
	};
	private OnWheelChangedListener mUnitListener = new OnWheelChangedListener()
	{
		public void onChanged(WheelView wheel, int oldValue, int newValue)
		{
			if (!timeScrolled)
			{
				mIndexUnit = newValue;
				if (oldValue < 16 && newValue >= 16)
				{
					mIndexCode = 0;
					codeCode.setAdapter(new ArrayWheelAdapter<String>(mArrEmpty));
					codeCode.setCurrentItem(0);
				}

				if (oldValue >= 16 && newValue < 16)
				{
					mIndexCode = 0;
					codeCode.setAdapter(new ArrayWheelAdapter<String>(mArrCommands));
					codeCode.setCurrentItem(0);
				}
				setProfileDetail();
			}
		}
	};
	private OnWheelChangedListener mCodeListener = new OnWheelChangedListener()
	{
		public void onChanged(WheelView wheel, int oldValue, int newValue)
		{
			if (!timeScrolled)
			{
				mIndexCode = newValue;
				setProfileDetail();
			}
		}
	};

	private OnWheelScrollListener mScrollListener = new OnWheelScrollListener()
	{
		public void onScrollingStarted(WheelView wheel)
		{
			timeScrolled = true;
		}

		public void onScrollingFinished(WheelView wheel)
		{
			timeScrolled = false;
			if (wheel == codeHome)
			{
				mHomeListener.onChanged(wheel, mIndexHome, wheel.getCurrentItem());
			} else if (wheel == codeUnit)
			{
				mUnitListener.onChanged(wheel, mIndexUnit, wheel.getCurrentItem());
			} else if (wheel == codeCode)
			{
				mCodeListener.onChanged(wheel, mIndexCode, wheel.getCurrentItem());
			}
		}
	};
}
